//
//  MessageFrame.m
//  ChatInterface_QQ
//
//  Created by admin on 2019/9/1.
//  Copyright © 2019 猫爷爷的酱醋茶. All rights reserved.
//

#import "MessageFrame.h"
#import "Message.h"
#import <UIKit/UIKit.h>
#import "NSString+NSStringEx.h"

@implementation MessageFrame

#pragma make - 重写message的setter方法
- (void)setMessage:(Message *)message
{
    _message = message;
    
    //计算每个子控件的frame和行高
    
    //获取屏幕宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    //设置统一的宽度
    CGFloat margin = 5;
    
    //时间Label的frame
    CGFloat timeLabelX = 0;
    CGFloat timeLabelY = 10;
    CGFloat timeLabelW = screenW;
    CGFloat timeLabelH = 15;
    
    if (!message.hideTime)
    {
        //如果需要显示时间，才需要计算时间的frame
          _timeFrame = CGRectMake(timeLabelX, timeLabelY, timeLabelW, timeLabelH);
    }
  
    
   
    //头像ImageView的frame
    CGFloat iconW = 40;
    CGFloat iconH = iconW;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX = self.message.type == MessageTypeMe ? (screenW - margin - iconW) : margin;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
   
    //消息Label的frame
    //1.先计算正文的大小
    CGSize textSize = [message.text sizeOfTextWithMaxSize:CGSizeMake(250, MAXFLOAT) font:textFont];
    CGFloat textW = textSize.width + 40;
    CGFloat textH = textSize.height + 30;
    //2.在计算，xy
    CGFloat textY = iconY;
    CGFloat textX = message.type == MessageTypeMe ? (screenW - margin - iconW - textW) : CGRectGetMaxX(_iconFrame);
    _messageFrame = CGRectMake(textX, textY, textW, textH);
    
    //行高
    //获取头像的最大Y值和正文的最大Y值，然后用最大的Y值+magrin
    CGFloat maxY = MAX(CGRectGetMaxY(_iconFrame), CGRectGetMaxY(_messageFrame));
    _rowHeight = maxY + margin;
    
 
}

@end
